<template>
	<view class="page">
    <view class="page-navbar" :style="'background-image: url('+indexBg+');'">
      <u-navbar :fixed="false" title="印章管理" placeholder bgColor="transparent"></u-navbar>
    </view>
    <view class="form-info">
      <view class="form-box u-m-b-48">
        <view class="form-item">
          <view class="form-lable required">印章名称</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.sealName" />
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable required">印章类型</view>
          <view class="form-content" @click="dictType = 'seal_type';dictName=['form', 'sealType']">
            <u--input placeholder="请选择" readonly border="none" inputAlign="right" v-model="form.sealType" />
          </view>
        </view>
        <!-- <view class="form-item">
          <view class="form-lable required">印章图片</view>
          <view class="form-content-right">
            <u-upload
              :fileList="form.sealPhoto"
              accept='image'
              name="sealPhoto"
              :sizeType="['compressed']"
              :maxCount="1"
              @delete="deletePic($event, 'form')"
              @afterRead="afterRead($event, 'form')"
              :previewFullImage="true"
            ></u-upload>
          </view>
        </view> -->
      </view>
      <view class="save-primary-btn-actions">
        <view class="save-primary-r-btn" @click="submit">保存提交</view>
      </view>
    </view>
    <u-picker :show="dictType!=''" :columns="[dict[dictType]]" keyName="lable" @confirm="pickerConfirm" @cancel="dictType = ''"></u-picker>
  </view>
</template>

<script>
import { getSealInfo, addSealInfo, updateSealInfo } from '@/api/index'
import { uploadImage } from '@/common/util'

export default {
  data() {
    return {
      indexBg: 'https://gowork365.com/prod-api/profile/mini/index/indexBg1.png',
      dictType: '',
      dictName: [],
      dict: {},
      id: '',
      form: {
        sealName: '',
        sealType: '',
      },
      defaultForm: {
        sealName: '',
        sealType: '',
      },
    }
  },
  onLoad(options) {
    this.getDicts(['seal_type'], this)
    if(options.id) {
      this.id = options.id
      this.getForm()
    }
  },
  methods: {
    getForm() {
      uni.showLoading({ mask: true })
      getSealInfo(this.id).then(response => {
        uni.hideLoading()
        if(response.data.sealPhoto) {
          response.data.sealPhoto = [ 
            {
              status: "success",
              id: this.id,
              img: this.baseUrl + response.data.sealPhoto,
              type: 'sealPhoto',
              url: this.baseUrl + response.data.sealPhoto
            }
          ]
        }
        this.form = response.data;
      });
    },
    submit() {
      const that = this
      let formFlag = false
      for(let key in this.defaultForm) {
        if(this.form[key] === '') {
          formFlag = true
        }
      }
      if(formFlag) {
        uni.showToast({
          icon: 'none',
          title: '请完善基础信息'
        })
        return
      }
      const params = {
        ...this.form,
      }
      // if(params.sealPhoto && params.sealPhoto.length) {
      //   params.sealPhoto = this.form.sealPhoto.map(v => v.url.replace(this.baseUrl, "")).join()
      // } else {
      //   uni.showToast({
      //     icon: 'none',
      //     title: '请上传印章'
      //   })
      //   return
      // }
      uni.showLoading({ mask: true })
      if (this.form.id != null) {
        updateSealInfo(params).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      } else {
        addSealInfo(params).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      }
    },
    deletePic(event, name, index=-1) {
      if(index > -1) {
        const info = this[name][index]
        info[event.name].splice(event.index, 1)
        this.$set(this[name], index, info)
        return
      }
      this[name][event.name].splice(event.index, 1);
    },
    // 新增图片
    async afterRead(event, name, index=-1) {
      let lists = [].concat(event.file);
      for (let i = 0; i < lists.length; i++) {
        const res = await uploadImage(lists[0]);
        if(res.code != 200) {
          return
        }
        if(index > -1) {
          const info = this[name][index]
          info[event.name] = [
            {
              status: "success",
              id: res.newFileName,
              img: res.fileName,
              type: event.name,
              url: this.baseUrl + res.fileName
            }
          ]
          this.$set(this[name], index, info)
          return
        } else {
          const img = [
            {
              status: "success",
              id: res.newFileName,
              img: res.fileName,
              type: event.name,
              url: this.baseUrl + res.fileName
            }
          ]
          this.$set(this[name], event.name, img)
        }
      }
    },
    pickerConfirm(e) {
      if(e.value[0].lable) {
        this[this.dictName[0]][this.dictName[1]] = e.value[0].lable
      } else {
        this[this.dictName[0]][this.dictName[1]] = ''
      }
      this.dictType = ''
      this.dictName = []
    },
  }
}
</script>

<style lang="less" scoped>
.page {
  width: 100%;
  min-height: 100vh;
  background: #F4F7F6;
  position: relative;
  padding-bottom: 188rpx;
  display: flex;
  flex-direction: column;
  .page-navbar {
    background-repeat: no-repeat;
    background-size: 750rpx 690rpx;
  }
  .form-info {
    flex: 1;
  }
}
</style>
